package ebj.common.redis.helper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.codec.digest.DigestUtils;

import redis.clients.jedis.Pipeline;

public class CSVHelper {
	private static CommonLogger logger = new CommonLogger(CSVHelper.class);
	
	public static void batchMultiFile(Pipeline pipeline, String csvFilePath) {
		File filePath = new File(csvFilePath);
		if (filePath.isDirectory()) {
			File[] files = filePath.listFiles();
			for (File file : files) {
				logger.info("load datas from file[" + file.getAbsolutePath() + "].");
				String appName = file.getName().split("_")[0];
				
				List<Map<String, String>> keyValpairs = CSVUtil.getKeyValPairsFromCSVFile(file);
				int counter = 0;
				for (Map<String, String> keyValPair : keyValpairs) {
					for (Entry<String, String> entry : keyValPair.entrySet()) {
						// String redis_key = "target_app_" +  entry.getValue();
						String redis_key_md5 = "target_app_" +  DigestUtils.md5Hex(entry.getValue());
						String redis_key_sha1 = "target_app_" +  DigestUtils.shaHex(entry.getValue());
						
//				Set<String> appNameSet = Sets.newHashSet();
//				String original_appNames_str = redisUtil.get(redis_key);
//				if (null != original_appNames_str) {
//					List<String> originalAppNameList = Lists.newArrayList(Splitter.on(",").split(original_appNames_str));
//					appNameSet.addAll(originalAppNameList);
//				}else {
//					// TODO nothing
//				}
//				appNameSet.add(appName);
//				String redis_val = Joiner.on(",").join(appNameSet);
//				pipeline.set(redis_key, redis_val);
						pipeline.set(redis_key_md5, appName);
						pipeline.set(redis_key_sha1, appName);
						// pipeline.expire(redis_key, 3600);  
					}
					counter++;
					if (counter % 10000 == 0) {
						logger.info("loaded [" + counter + "] datas.");
						pipeline.sync();  
					}
				}
				logger.info("loaded [" + counter + "] datas.");
				pipeline.sync();
			}
		}
	}
	
	public static void batchSingleFile1(Pipeline pipeline, String absoluteFileName) {
		File file = new File(absoluteFileName);
		logger.info("load datas from file[" + file.getAbsolutePath() + "].");
		String appName = file.getName().split("_")[0];
		
		BufferedReader bufferedReader=null;
        try { 
            bufferedReader = new BufferedReader(new FileReader(file));
            String line = "";
            int counter = 0;
            while ((line = bufferedReader.readLine()) != null) { 
            	String redis_key = "target_app_" + line;
            	pipeline.set(redis_key, appName);
            	// pipeline.expire(redis_key, 3600);
            	counter++;
    			if (counter % 10000 == 0) {
    				logger.info("loaded [" + counter + "] datas.");
    				pipeline.sync();  
    			}
            }
            logger.info("loaded [" + counter + "] datas.");
    		pipeline.sync();
        }catch (Exception e) {
        	logger.error("", e);
        }finally{
            if(bufferedReader!=null){
                try {
                    bufferedReader.close();
                    bufferedReader=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
	}
	
	public static void batchSingleFile(Pipeline pipeline, String csvFilePath) {
		File file = new File(csvFilePath);
		logger.info("load datas from file[" + file.getAbsolutePath() + "].");
		String appName = file.getName().split("_")[0];
		
		List<Map<String, String>> keyValpairs = CSVUtil.getKeyValPairsFromCSVFile(file);
		int counter = 0;
		for (Map<String, String> keyValPair : keyValpairs) {
			for (Entry<String, String> entry : keyValPair.entrySet()) {
				// String redis_key = "target_app_" +  entry.getValue();
				String redis_key_md5 = "target_app_" +  DigestUtils.md5Hex(entry.getValue());
				String redis_key_sha1 = "target_app_" +  DigestUtils.shaHex(entry.getValue());
				
//				Set<String> appNameSet = Sets.newHashSet();
//				String original_appNames_str = redisUtil.get(redis_key);
//				if (null != original_appNames_str) {
//					List<String> originalAppNameList = Lists.newArrayList(Splitter.on(",").split(original_appNames_str));
//					appNameSet.addAll(originalAppNameList);
//				}else {
//					// TODO nothing
//				}
//				appNameSet.add(appName);
//				String redis_val = Joiner.on(",").join(appNameSet);
//				pipeline.set(redis_key, redis_val);
				pipeline.set(redis_key_md5, appName);
				pipeline.set(redis_key_sha1, appName);
				// pipeline.expire(redis_key, 3600);  
			}
			counter++;
			if (counter % 10000 == 0) {
				logger.info("loaded [" + counter + "] datas.");
				pipeline.sync();  
			}
		}
		logger.info("loaded [" + counter + "] datas.");
		pipeline.sync();
	}
}
